package org.springsource.controllers;

import java.security.Principal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springsocial.domain.Country;
import org.springsocial.domain.Player;
import org.springsocial.domain.Team;
import org.springsocial.services.CountryService;
import org.springsocial.services.PlayerService;
import org.springsocial.services.TeamService;
import org.springsource.util.OrderExpression;
import org.springsource.util.PlayerOrderExpression;
import org.springsource.util.TeamEditor;

/**
 * Handles requests for the application home page.
 */
@Controller
public class AdminController {
	
	private static final Logger logger = LoggerFactory.getLogger(AdminController.class);
	
	
	
	@Autowired
	private TeamService teamService;
	
	@Autowired
	private PlayerService playerService;
	
	@Autowired
	private CountryService countryService;
	
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	
	@RequestMapping(value = "/admin", method = RequestMethod.GET)
	public String home(Principal currentUser, Model model) {
		logger.info("Welcome home! the client locale is ");
		
		List<Team> engTeams = teamService.getPremierLeagueTeams();
		List<Team> spanishTeams = teamService.getPrimeraDivisionTeams();
		
		model.addAttribute("engTeams", engTeams);
		model.addAttribute("spaTeams", spanishTeams);
		return "admin.home";
	}
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/admin/team/{teamName}/players", method = RequestMethod.GET)
	public String teamPlayers(Principal currentUser, Model model, @PathVariable("teamName") String teamName) {
		List<Player> players = teamService.getTeamWithPlayers(teamName).getOrderedPlayers(PlayerOrderExpression.FIRSTNAME, OrderExpression.DESC);
		model.addAttribute("players", players);
		return "admin.team";
	}
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/admin/player/add", method = RequestMethod.GET)
	public String addPlayer(Model model) {
		model.addAttribute("player", new Player());
		return "admin.player.add";
	}
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/admin/player/add", method = RequestMethod.POST)
	public String savePlayer(Model model, @ModelAttribute("player") Player player) {
		System.out.println(player.getFirstName());
		playerService.savePlayer(player);
		return "redirect:/admin";
	}
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/admin/player/{playerId}", method = RequestMethod.GET)
	public String editPlayer(Model model, @PathVariable Long playerId) {
		Player player = playerService.getPlayer(playerId);
		model.addAttribute("player", player);
		return "admin.player.edit";
	}
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/admin/player/edit", method = RequestMethod.POST)
	public String saveEditedPlayer(Model model, @ModelAttribute("player") Player player) {
		playerService.updatePlayer(player);
		return "redirect:/admin";
	}
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/admin/team/{teamName}/delete/{playerId}", method = RequestMethod.GET)
	public String deletePlayer(Model model, @ModelAttribute("player") Player player,@PathVariable("teamName")String teamName,@PathVariable("playerId")Long playerId) {
		Player playerToSave = playerService.getPlayer(new Long(playerId));
		playerService.deletePlayer(playerToSave);
		return "redirect:/admin";
	}
	
	@RequestMapping(value = "/admin/team/{teamId}", method = RequestMethod.GET)
	public String editTeam(Model model, @PathVariable Long teamId) {
		Team team = teamService.getTeam(teamId);
		model.addAttribute("team", team);
		return "admin.team.edit";
	}

	@ModelAttribute(value="countryList")
	public List<Country> countryList() {
		return countryService.getAll();
	}
	
	@ModelAttribute(value="teamList")
	public List<Team> teamList() {
		return teamService.getAll();
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		dateFormat.setLenient(true);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	binder.registerCustomEditor(Team.class, new TeamEditor(teamService));
	}

}
